package com.song.quartz.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.quartz.QuartzDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@Configuration
public class QuartzConfig {
    private final static String PREFIX = "spring.datasource.dynamic.datasource.quartz";

    @Bean
    @ConfigurationProperties(PREFIX)
    public DataSourceProperties schedulerProperties() {
        return new DataSourceProperties();
    }

    /**
     * 指定任务调度使用的数据库
     */
    @QuartzDataSource
    @Bean
    public DataSource quartzDataSource(DataSourceProperties schedulerProperties) {
        return schedulerProperties.initializeDataSourceBuilder().build();
    }

    @Bean
    public PlatformTransactionManager transactionManager(@Qualifier("quartzDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
